package com.chris.blog.controller;

import com.chris.blog.entity.po.SayPO;
import com.chris.blog.service.SayService;
import com.chris.blog.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

@Controller
public class SayController {

    @Autowired
    private SayService sayService;


    @GetMapping("say")
    public String saylist(Model model) {
        // 取最新的10条数据
        List<SayPO> saylist = sayService.selectAllDesc(10);
        model.addAttribute("saylist", saylist);
        return "page/say";
    }
    @PostMapping("say/{rid}")
    public String say(@PathVariable("rid") int rid, SayPO sayPO) {
        // 传入角色id 防止的是特殊手段的提交方式
        if (rid != 1) {
            return "redirect:/say";
        }
        // 否则提交内容
        Long id = SnowflakeIdWorker.nextId();
        sayPO.setId(id);
        sayPO.setGmtCreate(new Date());
        sayService.insertSelective(sayPO);

        return "redirect:/say";
    }
}
